Hugging Face
Hugging Face – The AI community building the future.
主に自然言語処理のコミュニティ、データセット
共通インタフェースもある
環境変数
Environment variables
HF_HOME 以下にモデルキャッシュされていく
HF_HUB_CACHE
デフォルトは ~/.cache/huggingface
キャッシュディレクトリの構造 Understand caching
HUGGING_FACE_HUB_TOKEN
$HF_HOME/token に書いていてもよい、環境変数セットしたら上書き
事前にモデルをダウンロードしておく
Downloading models
docker build 時など
ライブラリの Pipeline.from_pretrained を呼ぶ?
from huggingface_hub import hf_hub_download がある
cli がある、これでいいのか
huggingface_hub[cli]
code:dockerfile
ENV HF_HOME=/model-cache
RUN --mount=type=secret,id=HF_TOKEN HF_TOKEN=$(cat /run/secrets/HF_TOKEN) \
huggingface-cli download google/gemma-2-2b-it
ローカルにあるファイルだけを使う
ENV HF_HUB_OFFLINE=1
https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables#hfhuboffline
If set, no HTTP calls will be made to the Hugging Face Hub. If you try to download files, only the cached files will be accessed.
AutoModelHogeHoge.from_pretrained(local_files_only=True)
Hugging Face Transfer
爆速!!!
huggingface/hf_transfer
https://gyazo.com/526fd47362d544d36317d0ae7b133ba8
Environment variables
$ pip install huggingface_hub[hf_transfer] でインストール
CLI からも使える
$ HF_HUB_ENABLE_HF_TRANSFER=1 huggingface-cli upload ...
Docker image
huggingface's Profile | Docker Hub
Jupyter からログイン
使うのは huggingface_hub、huggingfaceではない
huggingface-hub · PyPI
code:notebook_login.py
from huggingface_hub import notebook_login
notebook_login()
notebook_login(token=userdata('hf_token')) # のように渡しても良い